package com.pignest.hotel.module.global;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;

import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.beans.MemberPrice;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;
import com.pignest.hotel.util.UiUtil;

public class MemberPriceWindow extends Window {
 
	private static final long serialVersionUID = 2147239867923229156L;
	private String id;
	private MemberLevel memberLevel;
	
	private MemberPrice memberPrice;

	private Intbox priceField;//会员房价
	private Listbox levelListbox;//
	private Listbox roomTypeListbox;//是否可自助退房
	


	private void init(){
		
	
		priceField = (Intbox)getFellow("priceField");
		levelListbox=(Listbox)getFellow("levelListbox");
		roomTypeListbox=(Listbox)getFellow("roomTypeListbox");
	
	}
	public String save(){
		init();
		int price=0;
		if(priceField.getValue()!=null){
			price=Integer.valueOf(MoneyUtil.dollar2cent(priceField.getValue()));
		}else{
			return "会员等级价格不能为空!";
		}
	

		MemberService server =Services.getMemberService();
		MemberPrice memberPrice= new MemberPrice();
		memberPrice.setPrice(price);
		
		String levelId=(String)levelListbox.getSelectedItem().getAttribute("value");
		MemberLevel level=server.getMemberLevel(levelId);
		memberPrice.setMemberLevel(level);

	    String roomTypeId=(String)roomTypeListbox.getSelectedItem().getAttribute("value");
	    
	    RoomType roomType =server.getRoomType(roomTypeId);
	    memberPrice.setRoomType(roomType);
		 if (StringUtils.isEmpty(id)) {//新增
				 String r=server.isOnlyTwoType(levelId, roomTypeId,null);
				 if(!r.equals("")){
					 return r;
				 }
				 server.addMemberPrice(memberPrice);
				
			 }else{
				 memberPrice.setId(id);
				 String r=server.isOnlyTwoType(levelId, roomTypeId,id);
				 if(!r.equals("")){
					 return r;
				 }
				server.updateMemberPrice(memberPrice);
			 }
		 UIUtils.refresh();
		return "";
	  }
	 public void setMemberPriceId(String id){
		 
		 try{
			 if(id!=null){
				 MemberPrice price =Services.getMemberService().getMemberPriceId(id);
			 if(price!=null){
				 this.id=id;
				 this.memberPrice=price;
			  }
			 }
			 setMemberPrice(memberPrice);
			 }catch(Exception e){
				 e.printStackTrace();
				 
			 }
		 }
		private void setMemberPrice(MemberPrice price){
			init();
			priceField.setValue(MoneyUtil.rand(MoneyUtil.cent2dollar(price.getPrice())));

			UiUtil.setListboxSelect(levelListbox,price.getMemberLevel().getId());
			UiUtil.setListboxSelect(roomTypeListbox,price.getRoomType().getId());
	 }
}
